Dreifing húsnæðislánategunda hjá íslenskum heimilum Dæmi um notkun Quarto og RStudio til að útbúa gagnvirkar HTML skýrslur
Code
temp <- tempfile ()
download.file (url = "https://sedlabanki.is/library/Skraarsafn/Fjarmalastodugleiki/FS/2022/isl_Kaflar.xlsx" ,
destfile = temp)
d <- read_excel (temp, sheet = "I-33" , skip = 11 ) |>
rename ("dags" = 1 ) |>
pivot_longer (c (- dags)) |>
separate (name, into = c ("tegund1" , "tegund2" ), sep = " og " )
Í nýjustu útgáfu af Fjármálastöðugleika birti Seðlabankinn tölur yfir hlutfall lánategunda hjá íslenskum heimilum. Ég hafði verið að spjalla við Ingva og Arnar hjá Pyngjunni um þessi gögn þ.a. ég útbjó snöggvast gagnvirka HTML skýrslu með Quarto í RStudio og sendi á kauðana.
Myndirnar hér eru teiknaðar með blöndu af ggplot2 og plotly. Hreyfið músina yfir stöplana til að sjá merkingar (hlutföll, tegund lána eða tegund vaxta) . Þið getið smellt á code flipana til að sjá undirliggjandi kóðann á bakvið skýrsluna. Kóðann í heild sinni má svo finna á githubinu mínu .
Svo er lítið mál að uppfæra slíkar skýrslur sjálfkrafa ef slóðin að gögnum sem eru notuð og uppsetning þeirra helst óbreytt.
Code
p <- d |>
mutate (dags = as_date (dags),
value = value / 100 ,
text = str_c ("Dagsetning: " , dags, " \n " ,
"Tegund láns: " , tegund1, " \n " ,
"Tegund vaxta: " , str_to_title (tegund2), " \n " ,
"Hlutfall: " , percent (value))) |>
ggplot (aes (dags, value, text = text)) +
geom_col (aes (fill = paste (tegund1, "og" , tegund2)), position = "stack" ) +
scale_x_date (date_labels = "%m/%y" ,
expand = expansion ()) +
scale_y_continuous (labels = label_percent (big.mark = "." ,
decimal.mark = "," ),
breaks = c (0 , 0.2 , 0.4 , 0.6 , 0.8 , 1 ),
expand = expansion ()) +
scale_fill_brewer (type = "qual" , palette = "Set1" ) +
theme_half_open () +
theme (legend.position = "none" ,
axis.text = element_text (size = 10 )) +
labs (x = NULL ,
y = NULL ,
fill = NULL ,
title = "Dreifing húsnæðislánategunda heimila" )
ggsave (plot = p +
theme (legend.position = "top" , legend.text = element_text (size = 11 )),
filename = "dreifing.png" ,
width = 8 , height = 0.5 * 8 , scale = 1.3 , bg = "white" )
ggplotly (p, tooltip = "text" )
Code
p <- d |>
mutate (dags = as_date (dags),
value = value / 100 ) |>
group_by (dags, tegund2) |>
summarise (value = sum (value),
.groups = "drop" ) |>
mutate (text = str_c ("Dagsetning: " , dags, " \n " ,
"Tegund láns: " , "Heild" , " \n " ,
"Tegund vaxta: " , str_to_title (tegund2), " \n " ,
"Hlutfall: " , percent (value))) |>
ggplot (aes (dags, value, text = text)) +
geom_col (aes (fill = paste (tegund2)), position = "stack" ) +
scale_x_date (date_labels = "%m/%y" ,
expand = expansion ()) +
scale_y_continuous (labels = label_percent (big.mark = "." ,
decimal.mark = "," ),
breaks = c (0 , 1 ),
expand = expansion ()) +
scale_fill_brewer (type = "qual" , palette = "Set1" ) +
theme_half_open () +
theme (legend.position = "none" ,
axis.text = element_text (size = 10 )) +
labs (x = NULL ,
y = NULL ,
title = "Dreifing vaxtategunda heimila" )
ggplotly (p, tooltip = "text" )
Code
p <- d |>
mutate (dags = as_date (dags)) |>
group_by (dags, tegund1) |>
summarise (value = sum (value/ 100 ),
.groups = "drop" ) |>
mutate (text = str_c ("Dagsetning: " , dags, " \n " ,
"Tegund láns: " , tegund1, " \n " ,
"Tegund vaxta: " , "Heild" , " \n " ,
"Hlutfall: " , percent (value))) |>
ggplot (aes (dags, value, text = text)) +
geom_col (aes (fill = paste (tegund1)), position = "stack" ) +
scale_x_date (date_labels = "%m/%y" ,
expand = expansion ()) +
scale_y_continuous (labels = label_percent (big.mark = "." ,
decimal.mark = "," ),
breaks = c (0 , 1 ),
expand = expansion ()) +
scale_fill_brewer (type = "qual" , palette = "Set1" ) +
theme_half_open () +
theme (legend.position = "none" ,
axis.text = element_text (size = 10 )) +
labs (x = NULL ,
y = NULL ,
title = "Dreifing lánategunda heimila" )
ggplotly (p, tooltip = "text" )
Code
plot_dat <- d |>
bind_rows (
d |>
mutate (tegund1 = "Heild" ) |>
group_by (dags, tegund1, tegund2) |>
summarise (value = sum (value),
.groups = "drop" )
) |>
bind_rows (
d |>
mutate (tegund2 = "Heild" ) |>
group_by (dags, tegund1, tegund2) |>
summarise (value = sum (value),
.groups = "drop" )
) |>
mutate (name = str_c (tegund1, " og " , tegund2),
dags = as_date (dags),
tegund2 = str_to_sentence (tegund2)) |>
group_by (name) |>
mutate (breyting = c (0 , diff (value))/ 100 ) |>
ungroup () |>
mutate (text = str_c ("Dagsetning: " , dags, " \n " ,
"Tegund láns: " , tegund1, " \n " ,
"Tegund vaxta: " , str_to_title (tegund2), " \n " ,
"Breyting: " , percent (breyting, suffix = "%-stig" )))
p <- plot_dat |>
ggplot (aes (dags, breyting, text = text)) +
geom_col (data = plot_dat |>
rename (t1 = tegund1, t2 = tegund2, nm = name) |>
filter (t1 != "Heild" , t2 != "Heild" ),
position = "stack" , fill = "grey80" , width = 28 ) +
geom_col (aes (fill = name), position = "stack" , width = 28 ) +
geom_hline (yintercept = 0 , lty = 2 ) +
scale_x_date (date_labels = "%m/%y" ) +
scale_y_continuous (labels = label_percent (suffix = "%-stig" ,
big.mark = "." ,
decimal.mark = "," ),
breaks = c (- 0.04 , - 0.02 , 0 , 0.02 , 0.04 )) +
scale_fill_brewer (type = "qual" , palette = "Set1" ) +
facet_grid (tegund2 ~ tegund1) +
theme_half_open () +
theme (legend.position = "none" ,
axis.text = element_text (size = 10 )) +
labs (x = NULL ,
y = NULL ,
title = "Mánaðarleg breyting á húsnæðislánategundum heimila" )
ggsave (plot = p, filename = "manbreyting.png" ,
width = 8 , height = 0.7 * 8 , scale = 1.3 , bg = "white" )
ggplotly (p, tooltip = "text" )